<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <el-button type="primary" @click="showAddDialog" size="medium">添加分类</el-button>
      <!-- 树形表格 -->
      <tree-table
        border
        :show-row-hover="false"
        :show-index="true"
        index-text="#"
        :expand-type="false"
        :selection-type="false"
        :data="cateList"
        :columns="columns">
        <template slot="isok" slot-scope="scope">
          <i v-if="!scope.row.cat_deleted" class="el-icon-success"></i>
          <i v-else class="el-icon-error"></i>
        </template>
        <template slot="order" slot-scope="scope">
          <el-tag v-if="scope.row.cat_level===0">一级</el-tag>
          <el-tag v-else-if="scope.row.cat_level===1" type="success">二级</el-tag>
          <el-tag v-else type="warning">三级</el-tag>
        </template>
        <template slot="opt">
          <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
        </template>
      </tree-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryData.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryData.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
      <!-- 添加分类显示对话 -->
      <el-dialog
        @close="resetAddCateForm"
        title="添加分类"
        :visible.sync="addCatedialogVisible"
        width="50%">
        <el-form ref="addCateRef" :rules="addCateFormRule" :model="addCateForm" label-width="100px">
          <el-form-item label="分类名称：" prop="cat_name">
            <el-input v-model="addCateForm.cat_name"></el-input>
          </el-form-item>
          <el-form-item label="父级分类：">
            <el-cascader
              v-model="casderValue"
              :options="parentCateList"
              :props="parentCateProps"
              @change="handleChange">
            </el-cascader>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="addCatedialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="setCate">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
  data () {
    return {
      queryData: {
        pagenum: 1,
        pagesize: 10
      },
      cateList: [],
      total: 0,
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        {
          label: '是否有效',
          // 这一列为模板列
          type: 'template',
          //  对应显示的模板内容
          template: 'isok'
        },
        {
          label: '排序',
          type: 'template',
          template: 'order'
        },
        {
          label: '操作',
          type: 'template',
          template: 'opt',
          width: '200px'
        }
      ],
      addCatedialogVisible: false,
      // 添加分类表单校验规则
      addCateFormRule: {
        cat_name: [
          { required: true, message: '请输入用户名称', trigger: 'blur' }
        ]
      },
      // 添加分类的表单数据
      addCateForm: {
        cat_name: ''
      },
      parentCateList: [],
      casderValue: [],
      parentCateProps: {
        checkStrictly: true,
        expandTrigger: 'hover',
        label: 'cat_name', // 将哪个属性值渲染到页面
        value: 'cat_id' // 将哪个属性保存下来
      }
    }
  },

  created () {
    this.getGoodsCate()
  },

  methods: {
    async getGoodsCate () {
      const { data: res } = await this.$axios.get('categories', {
        params: this.queryData
      })
      if (res.meta.status !== 200) return this.$message.error('获取商品数据列表失败')
      // console.log(res.data)
      this.cateList = res.data.result
      this.total = res.data.total
    },
    handleSizeChange (newSize) {
      this.queryData.pagesize = newSize
      this.getGoodsCate()
    },
    handleCurrentChange (newCurrent) {
      this.queryData.pagenum = newCurrent
      this.getGoodsCate()
    },
    // 点击添加分类
    async showAddDialog () {
      this.addCatedialogVisible = true
      this.getParentGoodsCate()
    },
    async getParentGoodsCate () {
      const { data: res } = await this.$axios.get('categories', { params: { type: 2 } })
      // console.log(res)
      if (res.meta.status !== 200) return this.$message('获取父级分类失败')
      this.parentCateList = res.data
    },
    // 级联选择器发生变化
    handleChange () {
      // console.log(this.casderValue)
      if (this.casderValue.length > 0) {
        // 父级id
        this.addCateForm.cat_pid = this.casderValue[this.casderValue.length - 1]
        // 当层层级
        this.addCateForm.cat_level = this.casderValue.length
      } else {
        this.addCateForm.cat_pid = 0
        this.addCateForm.cat_level = 0
      }
    },
    async setCate () {
      // console.log(this.addCateForm)
      const { data: res } = await this.$axios.post('categories', this.addCateForm)
      if (res.meta.status !== 201) return this.$message.error('添加分类失败')
      this.$message.success('添加分类成功')
      this.addCatedialogVisible = false
      this.getGoodsCate()
    },
    resetAddCateForm () {
      // 重置表单数据
      this.$refs.addCateRef.resetFields()
      // 清除addCateForm数据
      this.addCateForm.cat_pid = 0
      this.addCateForm.cat_level = 0
      // 清除级联选择器保存的数据
      this.casderValue = []
    }
  }
}
</script>

<style scoped lang='less'>
i{
  color: green;
}
.el-cascader{
  width: 100%;
  box-sizing: border-box;
}
</style>
